/*
 * @Author: wq<779508400@qq.com>
 * @Date: 2021-11-03 09:54:42
 * @LastEditTime: 2021-11-03 11:18:01
 * @LastEditors: Please set LastEditors
 * @Description: string slit for delims
 * @FilePath: /string_split/src/string_split.c
 */

#include "string_split.h"
#include <string.h>

#define RUN_DEBUG

struct _DelimNumber
{
    int value;
    struct _DelimNumber* next;
};
typedef struct _DelimNumber DelimNumber;
DelimNumber* delimnumber_head = NULL;
DelimNumber* delimnumber_end = NULL;
DelimNumber* delimnumber_next = NULL;

static void delimnumber_expand(int value)
{
    DelimNumber *delimnumber = (DelimNumber*)malloc(sizeof(DelimNumber));
    delimnumber->value = value;
    delimnumber->next = NULL;

    if(delimnumber_head == NULL)
    {
        delimnumber_head = delimnumber;
    }
    else
    {
        delimnumber_end->next = delimnumber;
    }
    delimnumber_end = delimnumber;
}

static void delimnumber_free()
{
    DelimNumber *delimnumber = delimnumber_head;
    while(delimnumber_head->next)
    {
        DelimNumber * p = delimnumber_head;
        delimnumber_head = p->next;
        free(p);
    }
    delimnumber_head = NULL;
    delimnumber_end = NULL;
    delimnumber_next = NULL;
}

static void delimnumber_next_value()
{
    if(delimnumber_next->next == NULL)
        return;
    delimnumber_next = delimnumber_next->next;
}

/**
 * @name: 获取字符串分隔符数量
 * @msg: 
 * @param {char*} src_strings 字符串输入源
 * @param {char} delim 分隔符
 * @param {int} *pos 分隔符位置值
 * @return {*} 分隔符数量
 */
static int get_delim_number(char* src_strings, char delim)
{
    int delim_number = 0, i = 0;
    delimnumber_expand(i);
    while((*src_strings) && (*src_strings) != '\0')
    {
        if(*src_strings == delim)
        {
            delim_number++;
            delimnumber_expand(i + 1);
        }
        i++;
        src_strings++;
    }
    return delim_number;
}

/**
 * @name: 获取字符串分隔符数量
 * @msg: 
 * @param {char*} src_strings 字符串输入源
 * @param {char*} delims 分隔符
 * @param {int} *pos 分隔符位置值
 * @return {*} 分隔符数量
 */
static int get_delims_number(char* src_strings, char* delims)
{
    if(delims == NULL)
        return 0;
    int delims_length = strlen(delims);
    int delim_number = 0, i = 0;
    delimnumber_expand(i);
    while((*src_strings) && (*src_strings) != '\0')
    {
        for(int j = 0; j < delims_length; j++)
        {
            if(*src_strings == delims[j])
            {
                delim_number++;
                delimnumber_expand(i + 1);
                break;
            }
        }
        i++;
        src_strings++;
    }
    return delim_number;
}

/**
 * @name: 字符串分割处理
 * @msg: 
 * @param {char} delim 分隔符
 * @param {char} *src 字符串输入源
 * @return {*} 分隔符结构体
 */
StringSplit* string_split_handle(char delim, char *src)
{
    int delim_number = get_delim_number(src, delim);
    int sub_str_number = (delimnumber_end->value - 1 == (strlen(src) - 1 )) ? delim_number : delim_number + 1;
    #ifdef RUN_DEBUG
        printf("delim_number:%d, sub_str_number:%d\n", delim_number, sub_str_number);
    #endif
    int length = strlen(src);
    int size = length - delim_number + 2 * sizeof(int) + 3 * sub_str_number * sizeof(int);
    StringSplit* string_split = (StringSplit*)malloc(sizeof(StringSplit));
    string_split->size = size;
    string_split->number = sub_str_number;
    string_split->items = calloc(sub_str_number, sizeof(StringSplitItem*));
    delimnumber_next = delimnumber_head;
    for(int i = 0; i < sub_str_number; i++)
    {
        string_split->items[i] = (StringSplitItem*)malloc(sizeof(StringSplitItem));
        StringSplitItem* item = string_split->items[i];
        item->pos = delimnumber_next->value;
        item->length = (i + 1 == sub_str_number) ? length - item->pos : delimnumber_next->next->value - delimnumber_next->value - 1;
        item->size = 3 * sizeof(int) + item->length;
        item->str = calloc((size_t)item->length + 1, sizeof(char));
        memset(item->str, 0, item->length + 1);
        memcpy(item->str, src + item->pos, item->length);
        #ifdef RUN_DEBUG
            printf("index:%d, pos:%d, length:%d, size:%d, str:%s\n", i, item->pos, item->length, item->size, item->str);
        #endif
        delimnumber_next_value();
    }
    #ifdef RUN_DEBUG
        printf("Ending Split\n");
    #endif
    return  string_split;
}

/**
 * @brief 
 * 
 * @param delims
 * @param src 
 * @return StringSplit* 
 */
StringSplit* string_delims_split_handle(char* delims, char *src)
{
    int delim_number = get_delims_number(src, delims);
    int sub_str_number = (delimnumber_end->value - 1 == (strlen(src) - 1 )) ? delim_number : delim_number + 1;
    #ifdef RUN_DEBUG
        printf("delim_number:%d, sub_str_number:%d\n", delim_number, sub_str_number);
    #endif
    int length = strlen(src);
    int size = length - delim_number + 2 * sizeof(int) + 3 * sub_str_number * sizeof(int);
    StringSplit* string_split = (StringSplit*)malloc(sizeof(StringSplit));
    string_split->size = size;
    string_split->number = sub_str_number;
    string_split->items = calloc(sub_str_number, sizeof(StringSplitItem*));
    delimnumber_next = delimnumber_head;
    for(int i = 0; i < sub_str_number; i++)
    {
        string_split->items[i] = (StringSplitItem*)malloc(sizeof(StringSplitItem));
        StringSplitItem* item = string_split->items[i];
        item->pos = delimnumber_next->value;
        item->length = (i + 1 == sub_str_number) ? length - item->pos : delimnumber_next->next->value - delimnumber_next->value - 1;
        item->size = 3 * sizeof(int) + item->length;
        item->str = calloc((size_t)item->length + 1, sizeof(char));
        memset(item->str, 0, item->length + 1);
        memcpy(item->str, src + item->pos, item->length);
        #ifdef RUN_DEBUG
            printf("index:%d, pos:%d, length:%d, size:%d, str:%s\n", i, item->pos, item->length, item->size, item->str);
        #endif
        delimnumber_next_value();
    }
    #ifdef RUN_DEBUG
        printf("Ending Split\n");
    #endif
    return  string_split;   
}

/**
 * @name: 字符串释放
 * @msg: 
 * @param {StringSplit} *splits StringSplit指针
 * @return {*}
 */
void string_split_free(StringSplit *splits)
{
    if(splits == NULL)
        return;
    if(splits->number > 0 && splits->items != NULL)
    {
        for(int i = 0; i < splits->number; i++)
        {
            free(splits->items[i]->str);
            free(splits->items[i]);
        }
    }
    free(splits);
    delimnumber_free();
}

/**
 * @description: 输出字符串打印测试
 * @param {*} 输出字符串指针
 * @return {*}
 */
void string_split_output_test(StringSplit *splits)
{
    printf("splits: number:%d, size:%d \n", splits->number, splits->size);
    if(splits->items != NULL)
        for(int i = 0; i < splits->number; i++)
        {
            StringSplitItem* item = splits->items[i];
            if(item != NULL)
                printf("output: index:%d, pos:%d, length:%d, size:%d, str:%s\n", i, item->pos, item->length, item->size, item->str);
            else
                printf("item:%d is NULL\n", i);
        }
    else
        printf("splits->item == NULL\n");
}
